import { cloneDeep, } from 'lodash-es';

let jobList: JobRecord[] = [
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": null,
    "updateTime": null,
    "remark": "",
    "jobId": 1,
    "jobName": "系统默认（无参）",
    "jobGroup": "DEFAULT",
    "invokeTarget": "ryTask.ryNoParams",
    "cronExpression": "0/10 * * * * ?",
    "misfirePolicy": "3",
    "concurrent": "1",
    "status": "1",
    "nextValidTime": "2024-07-11 00:31:20"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": null,
    "updateTime": null,
    "remark": "",
    "jobId": 2,
    "jobName": "系统默认（有参）",
    "jobGroup": "DEFAULT",
    "invokeTarget": "ryTask.ryParams('ry')",
    "cronExpression": "0/15 * * * * ?",
    "misfirePolicy": "3",
    "concurrent": "1",
    "status": "1",
    "nextValidTime": "2024-07-11 00:31:30"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": null,
    "updateTime": null,
    "remark": "",
    "jobId": 3,
    "jobName": "系统默认（多参）",
    "jobGroup": "DEFAULT",
    "invokeTarget": "ryTask.ryMultipleParams('ry', true, 2000L, 316.50D, 100)",
    "cronExpression": "0/20 * * * * ?",
    "misfirePolicy": "3",
    "concurrent": "1",
    "status": "1",
    "nextValidTime": "2024-07-11 00:31:20"
  }
];

const mocks: MockRecord[] = [
  {
    method: "get",
    url: "/api/monitor/job/list",
    response(option) {
      const copyList = cloneDeep(jobList);

      let resList = jobList;

      if (option.query.pageNum) {
        resList = copyList.splice((option.query.pageNum - 1) * option.query.pageSize, option.query.pageSize);
      }

      const resData: responseType = {
        code: 200,
        msg: "查询成功",
        total: jobList.length,
        ...(option.query.pageNum ? { rows: resList } : { data: resList })
      }

      return resData;
    }
  },
  {
    method: "get",
    url: "/api/monitor/job/:jobId",
    response(option) {
      const resJob = jobList.find(item => item.jobId == option.query.jobId);

      const resData: responseType = {
        code: resJob ? 200 : 500,
        msg: resJob ? "查询成功" : "查询失败",
        data: resJob,
      }

      return resData;
    }
  },
  {
    method: "post",
    url: "/api/monitor/job",
    response(option) {
      const newJob = option.body;

      newJob.jobId = jobList.length + 1;
      jobList.unshift(newJob);

      const resData: responseType = {
        code: 200,
        msg: "新增成功",
      }

      return resData;
    }
  },
  {
    method: "put",
    url: "/api/monitor/job",
    response(option) {
      const newJob = option.body;

      const index = jobList.findIndex(item => item.jobId == newJob.jobId);

      jobList.splice(index, 1, newJob);

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
  {
    method: "delete",
    url: "/api/monitor/job/:jobIds",
    response(option) {
      const jobIds = option.query.jobIds.split(',');

      const filterList = jobList.filter(item => !jobIds.includes(String(item.jobId)));

      jobList = filterList;

      const resData: responseType = {
        code: 200,
        msg: "删除成功",
      }
      return resData;
    }
  },
  {
    method: "put",
    url: "/api/monitor/job/run",
    response() {

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
];

export default mocks;